//
//  JSSTabBar.m
//  02weiBo
//
//  Created by jss on 16/4/24.
//  Copyright © 2016年 jss. All rights reserved.
//

#import "JSSTabBar.h"


@interface JSSTabBar()

@property(nonatomic,strong)UIButton *centerBtn;

@end

@implementation JSSTabBar



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        [self setupUI];
        
    }
    return self;
}


//设置UI界面
-(void)setupUI {
    
    //添加中间的加号button
    UIButton *centerBtn = [[UIButton alloc]init];
    
    self.centerBtn = centerBtn;
    
    //设置图片
    [centerBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [centerBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    //设置背景图片
    [centerBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [centerBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //给centeBtn添加监听事件
    [centerBtn addTarget:self action:@selector(didClickCenterBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [centerBtn sizeToFit];
    
    [self addSubview:centerBtn];
}


//实现Centerbtn的监听方法
-(void)didClickCenterBtn:(UIButton *)btn {
    
   // NSLog(@"加号按钮被点击了");
    //发出通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"dianji" object:nil];
    
    
}



-(void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat btnX = self.bounds.size.width / 2;
    CGFloat btnY = self.bounds.size.height / 2;
    
    //设置centerBtn的中心点就是当前自定义的这个Table的中心
    self.centerBtn.center = CGPointMake(btnX, btnY);
    
    CGFloat btnW = self.bounds.size.width / 5;
 
   // NSLog(@"%ld",self.subviews.count);
 
    int index = 0;
    
    for (UIView *view in self.subviews) {
        
        Class v = NSClassFromString(@"UITabBarButton");
        
        if ([view isKindOfClass:v]) {
            // NSLog(@"此方法被执行了");
            
            //设置 遍历
            
            CGRect vv = view.frame;
            
            vv.size.width= btnW;
            
            view.frame = vv;
            
            CGRect vv1 = view.frame;
            
            vv1.origin.x = index * btnW;
            
            view.frame = vv1;
            
            index++;
            
            //当index等于2的时候 让index加一  自动的把第三个按钮往后面移一个位置
            if (index == 2) {
                
                index++;
            }
            
        }
    }
    
}

@end
